package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 北青网> 解析类
 *
 * @author KK
 *
 */
public class YnetNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {


		String[] regex={
				"http://.*[.]ynet.com/.*3.1/[0-9]{4}/[0-9]{2}/[0-9]+[_0-9]*.html",
				"http://.*[.]ynet.com/[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+@[0-9]+[_0-9]*.htm",
				"http://bjyouth.ynet.com/[0-9]*.[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]+[_0-9]*.html",
				"http://tom[.].*[.]ynet[.]com/.*/[0-9]{4}/[0-9]{2}/[0-9]+[_0-9]*[.]html.*",
		};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		String author="";
		title=doc.select("div.va h1").text();
		String text=doc.select("div.va p.fs12").text();
		  Elements select=doc.select("div#pzoom");
		 select.select(".vcopyurl").remove();
		 content=HtmlCleaner.getContentHtml(url,select);
		date=DateUtils.matchDate(text);

		if(StringUtils.isBlank(title)){
			title=doc.select("div.pagecontent h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.endText"));
			date=DateUtils.matchDate(text);
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select("div.article h1").text();
			text=doc.select("div.byline").text();
			select=doc.select("div.article");
			select.select(".byline,h1").remove();
			content=HtmlCleaner.getContentHtml(url,select);
			date=DateUtils.matchDate(text);
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select("div.description h1").text();
			doc.select("div.description h1").remove();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.description"));
			if(url.contains("/3.1/")){
				String after=StringUtils.substringAfter(url, "/3.1/");
				after=StringUtils.substringBeforeLast(after, "/");
				date=DateUtils.matchDate(after);
			}else{
				date=DateUtils.matchDate(url);
			}
		}
		
		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源:");
		}
		author= JSoupUtils.matchAuthor(doc, "来源:");
		NewsMeta news=new NewsMeta();
		news.setUrl(url);
		news.setType(1);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(NumberUtils.parseInt(commnetNum));
		news.setClickNum(NumberUtils.parseInt(clickNum));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);



		news.setAuthor(author);
		return news;

	}

	public static void main(String[] args) {


		YnetNewsAnalyse a=new YnetNewsAnalyse();
//		String dateStr="http://8gua.ynet.com/3.1/1204/23/7018549.html";
//		System.out.println(a.parseUrlDate(dateStr));
		String url="http://msn.ynet.com/3.1/1407/18/9184229.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
